package gw.com.android.ui.onlive.component;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

public class ActiveButton extends FrameLayout {

    private int currentStatu = STATU_ADD_FOLLOW;
    public static final int STATU_ADD_FOLLOW = 0X11;
    public static final int STATU_ADDING = 0X12;
    public static final int STATU_FOLLOWED = 0X13;

    private static final int STYLE_1 = 0x001;// 样式1
    private static final int STYLE_2 = 0x002;// 样式2

    public ActiveButton(Context context) {
        this(context, null);
    }

    public ActiveButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ActiveButton(Context mContext, AttributeSet attrs, int defStyleAttr) {
        super(mContext, attrs, defStyleAttr);
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.ActiveButton);
        int styleInt;
        try {
            styleInt = typedArray.getInt(R.styleable.ActiveButton_style_int, STYLE_1);
        } finally {
            typedArray.recycle();
        }
        init(mContext, styleInt);
    }


    public int getCurrentStatu() {
        return currentStatu;
    }

    TextView tv_add_follow, tv_followed;
    ProgressBar progressBar;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void init(Context context, int styleInt) {
        //创建3个元件
        FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        FrameLayout.LayoutParams params3 = new FrameLayout.LayoutParams(dp2px(20), dp2px(20));
        params2.gravity = Gravity.CENTER;
        params3.gravity = Gravity.CENTER;
        //第一个，+关注-按钮
        //第二个，progressBar
        //第三个，已关注-按钮
        //这里加载的是默认布局
        tv_add_follow = new TextView(context);
        tv_add_follow.setText("+ 关注");
        tv_add_follow.setTextSize(13);
        tv_add_follow.setIncludeFontPadding(false);
        tv_add_follow.setGravity(Gravity.CENTER);
        addView(tv_add_follow, params2);

        progressBar = (ProgressBar) LayoutInflater.from(context).inflate(R.layout.progress_add_follow, null);
        progressBar.setVisibility(GONE);
        addView(progressBar, params3);

        tv_followed = new TextView(context);
        tv_followed.setText("已关注");
        tv_followed.setTextSize(13);
        tv_followed.setIncludeFontPadding(false);
        tv_followed.setGravity(Gravity.CENTER);
        tv_followed.setVisibility(GONE);
        addView(tv_followed, params2);

        switch (styleInt) {
            case STYLE_1:
                tv_add_follow.setTextColor(Color.parseColor("#F8A916"));
                tv_add_follow.setBackground(ContextCompat.getDrawable(context, R.drawable.active_button_background));
                tv_followed.setTextColor(Color.parseColor("#999999"));
                tv_followed.setBackground(ContextCompat.getDrawable(context, R.drawable.active_button_background4));
                break;
            case STYLE_2:
                tv_add_follow.setTextColor(Color.parseColor("#FFFFFF"));
                tv_add_follow.setBackground(ContextCompat.getDrawable(context, R.drawable.active_button_background2));
                tv_followed.setTextColor(Color.parseColor("#FFFFFF"));
                tv_followed.setBackground(ContextCompat.getDrawable(context, R.drawable.active_button_background3));
                break;
        }
        //setBackground(ContextCompat.getDrawable(context, R.drawable.active_button_background5));
    }


    //开放切换状态的接口

    /**
     * 切换到已关注
     */
    public void switchToFollowed() {
        currentStatu = STATU_FOLLOWED;
        tv_add_follow.setVisibility(GONE);
        progressBar.setVisibility(GONE);
        tv_followed.setVisibility(VISIBLE);
    }

    /**
     * 切换到+关注
     */
    public void switchToAddFollow() {
        currentStatu = STATU_ADD_FOLLOW;
        tv_add_follow.setVisibility(VISIBLE);
        progressBar.setVisibility(GONE);
        tv_followed.setVisibility(GONE);
    }

    /**
     * 切换到请求中
     */
    public void switchToProgressBar() {
//        currentStatu = STATU_ADDING;
        tv_add_follow.setVisibility(GONE);
        progressBar.setVisibility(VISIBLE);
        tv_followed.setVisibility(GONE);
    }

    private int dp2px(float dp) {
        return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
    }
}
